package com.student.education.service.impl;


import com.github.pagehelper.PageHelper;
import com.student.education.common.CommonPage;
import com.student.education.dao.AttentionStudentDao;
import com.student.education.service.AttentionStudentService;
import com.student.education.to.UserTo;
import com.student.education.vo.UserQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AttentionStudentServiceImpl implements AttentionStudentService {

    @Autowired
    AttentionStudentDao attentionStudentDao;

    @Override
    public boolean addStudents(Integer tId, Integer sId) {
        //查询之前是否关注过这个学生
        Integer count = attentionStudentDao.queryHave(tId,sId);
        if (count>0){
            return false;
        }
        return attentionStudentDao.addStudents(tId,sId)>0;
    }

    @Override
    public CommonPage<UserTo> queryAll(UserQueryVo vo) {
        if (vo.getPageUtil() != null) {
            PageHelper.startPage(vo.getPageUtil().getPageNumber(), vo.getPageUtil().getPageSize());
        }
        List<UserTo> userTos = attentionStudentDao.queryAll(vo);
        for (int i =0;i<userTos.size();i++){
            if (Objects.equals(userTos.get(i).getClassId(),null)){
                continue;
            }
            userTos.get(i).setClassIdList(Arrays.asList(userTos.get(i).getClassId().split(","))
                    .stream().map(s -> Integer.parseInt(s.trim()))
                    .collect(Collectors.toList()));
        }
        return CommonPage.restPage(userTos);
    }

    @Override
    public boolean deleteAttentionStudents(Integer userId, Integer id) {
        return attentionStudentDao.deleteAttentionStudents(userId,id)>0;
    }


}
